package com.wgo.precise.server.wiring;

import com.wgo.bpot.server.persist.PersistService;
import com.wgo.bpot.server.persist.Persistent;
import com.wgo.bpot.server.persist.UniqueValue;
import com.wgo.bpot.server.persist.UniqueValueFactoryImpl;
import com.wgo.bpot.server.persist.hibernate.HibernatePersistService;
import com.wgo.bpot.server.persist.trigger.TriggerRegistry;
import com.wgo.bpot.server.persist.trigger.TriggerRegistryImpl;
import com.wgo.bpot.server.service.ModifyingOperationsState;
import com.wgo.bpot.wiring.ServiceAccessor;
import com.wgo.precise.server.pdf.CategoryPdfFactory;
import com.wgo.precise.server.pdf.PdfFactoryRegistry;
import com.wgo.precise.server.pdf.ProjectPdfFactory;
import com.wgo.precise.server.pdf.RequirementPdfElementFactory;
import com.wgo.precise.server.trigger.BeforeSaveOrUpdatePropertySetTrigger;
import com.wgo.precise.server.trigger.BeforeSaveRequirementTrigger;
import com.wgo.precise.server.trigger.BeforeUpdateRequirementTrigger;
import com.wgo.precise.server.trigger.RetrieveRequirementTrigger;

/**
 * @version $Id: PreciseServiceAccessor.java 540 2008-01-09 19:42:31Z petter.eide $
 */
public interface PreciseServiceAccessor extends ServiceAccessor<Persistent, HibernatePersistService<Persistent>> {
	
	public PdfFactoryRegistry getPdfFactoryRegistry();
	public String getPdfEndOfPageText();

	public class Factory {
		
		public static PreciseServiceAccessor createPreciseServiceAccessor(){
			PreciseServiceAccessorImpl serviceAccessor = new PreciseServiceAccessorImpl();
			HibernatePersistService<Persistent> persistService = new HibernatePersistService<Persistent>();
			persistService.setDomainModelConvention(new PreciseEjbModelConvention());//(RequirementSystem.class);
			UniqueValueFactoryImpl uniqueValueFactory = new UniqueValueFactoryImpl();
			uniqueValueFactory.setUniqueValueClass(UniqueValue.class);
			uniqueValueFactory.setPersistService(persistService);
			persistService.setUniqueValueFactory(uniqueValueFactory);
	        serviceAccessor.setDomainModelConvention(persistService.getDomainModelConvention());
			serviceAccessor.setPersistService(persistService);
			TriggerRegistry triggerRegistry = createTriggerRegistry(persistService);
			serviceAccessor.setTriggerRegistry(triggerRegistry);
	        PdfFactoryRegistry pdfFactoryRegistry = createPdfFactoryRegistry(persistService);
			serviceAccessor.setPdfFactoryRegistry(pdfFactoryRegistry);
			serviceAccessor.setPdfEndOfPageText("Precise Requirement System");
	        serviceAccessor.setUserRoleRegistry(PreciseUserRoleRegistry.REGISTRY);
			serviceAccessor.setInitializationService(new PreciseInitializationServiceImpl(persistService));
	        ModifyingOperationsState modifyingOperationsState = null;//new ModifyingOperationsState(new Date(),10000, persistService.getDomainModelConvention());
			serviceAccessor.setServicesFactory(new PreciseServicesFactoryImpl(serviceAccessor,modifyingOperationsState));
			return serviceAccessor;
		}

		private static PdfFactoryRegistry createPdfFactoryRegistry(PersistService<Persistent> persistService) {
			PdfFactoryRegistry pdfFactoryRegistry = new PdfFactoryRegistry();
			pdfFactoryRegistry.setPersistService(persistService);
			pdfFactoryRegistry.addFactory(new CategoryPdfFactory());
			pdfFactoryRegistry.addFactory(new ProjectPdfFactory());
			pdfFactoryRegistry.addFactory(new RequirementPdfElementFactory());
			return pdfFactoryRegistry;
		}

		private static TriggerRegistry createTriggerRegistry(PersistService<Persistent> persistService) {
			TriggerRegistry triggerRegistry = new TriggerRegistryImpl(persistService);
			triggerRegistry.addTrigger(new BeforeSaveRequirementTrigger());
			triggerRegistry.addTrigger(new BeforeSaveOrUpdatePropertySetTrigger());
			triggerRegistry.addTrigger(new BeforeUpdateRequirementTrigger());
			triggerRegistry.addTrigger(new RetrieveRequirementTrigger());
			return triggerRegistry;
		}
		
	}

}
